package cn.zqy.services;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Properties;
import java.util.logging.Logger;

public class ConfigService
{
	private static String agePath = "/config/";//google app server
	private static String localPath = "WEB-INF/config/";//local
	private static HashMap<String,Properties> configMap=new HashMap<String,Properties>();	
	private static final Logger log= Logger.getLogger(ConfigService.class.getName());
	
	public static String getValue(String file,String key)
	{
		load(file);
		if(configMap.get(file)==null)
		{
			return "";
		}
		else
		{
			return (String)configMap.get(file).get(key);
		}
	}
	
	private static void load(String file)
	{		
		if(configMap.containsKey(file))
		{
			return;
		}
		try
		{
			FileInputStream in=new FileInputStream(localPath+file);
			URL url=ConfigService.class.getResource(agePath+file);
			if(url!=null)
			{
				in=new FileInputStream(url.getFile());
			}
			Properties properties = new Properties();
			properties.load(in);
			configMap.put(file, properties);
		}
		catch (FileNotFoundException e)
		{
			log.severe(e.toString());
		}
		catch (IOException e)
		{
			log.severe(e.toString());
		}
		
	}
}
